<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>view/workArea/parts/applyPartsEdit</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<link rel="stylesheet" href="../../../css/mui.min.css">
		<link rel="stylesheet" href="../../../css/global.css">
		<link rel="stylesheet" href="../../../css/style.css">
		<link rel="stylesheet" href="../../../css/mui.picker.css">
		<link rel="stylesheet" href="../../../css/mui.poppicker.css">
		
		<style>
			.mui-radio input[type=radio] {
				left: 0;
				top: 11px;
			}
		
			.mui-checkbox label,
			.mui-radio label {
				padding-left: 38px;
				font-size: 14px;
				color: #333333;
				padding-right: 0;
			}
		
			.d_ipt {
				width: 65% !important;
			}
		
			input[type=number],
			input[type=text] {
				padding: 10px 0 !important;
			}
		
			.clum_textarea {
				margin: 0 20px;
				font-size: 16px;
				color: #999999;
				border-bottom: 1px solid #e8e8e8;
				height: 100px;
				overflow: hidden;
			}
		
			.clum_textarea .name {
				float: left;
				display: block;
				line-height: 50px;
			}
		
			.clum_textarea textarea {
				text-align: right;
				float: right;
				height: 100px;
				min-height: 100px;
				padding: 14px 0;
			}
		</style>
		<style>
			.mui-btn {
				font-size: 16px;
				padding: 8px;
				margin: 3px;
			}
			h5.mui-content-padded {
				margin-left: 3px;
				margin-top: 20px !important;
			}
			h5.mui-content-padded:first-child {
				margin-top: 12px !important;
			}
			.ui-alert {
				text-align: center;
				padding: 20px 10px;
				font-size: 16px;
			}
			
			
			.mui-btn-select{
				line-height: 3.125rem;
			}
			.sel{
				font-size: 12px;
				margin-left: 10px;
				color: #ffffff;
				background: #ea1e1a;
				height: 20px;
				line-height: 12px !important;
				margin-top: 14px;
				border-radius: 5px;
				padding: 5px;
			}
			.selectxx{
				position: absolute;
				right: 5px;
			}
			.details_con {
			    margin: 20px;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 id="title" class="mui-title">修改备件</h1>
		</header>
		<div class="mui-content">
			<input type="hidden" id="itemIndex" />
			<div class="details_con" id="form_info">
				<div class="clum">
					<span class="name">备件号<em class="t_red">*</em></span>
					<span class="sel selectxx" id="selectDetailsBtn">查询</span>		
					<input type="text" id="Part_no" class="d_ipt" disabled="disabled" required="required" placeholder="请输入备件号" />
					<input type="hidden" id="Part_id"/>
				</div>
				<div class="clum nobor autoHeight">
					<span class="name">描述（中文）<em class="t_red">*</em></span>
					<div class="clear"></div>
					<textarea class="textarea" id="Part_name" required="required" placeholder="请输入描述（中文）"></textarea>
				</div>
				<div class="clum">
					<span class="name">备件数量<em class="t_red">*</em></span>
					<input type="number" id="Part_qty" class="d_ipt" required="required" placeholder="请输入备件数量" />
					<!-- <div class="mui-numbox" data-numbox-min='0' data-numbox-max='0'>
						<button class="mui-btn mui-btn-numbox-minus" type="button">-</button>
						<input id="Part_qty" class="mui-input-numbox" type="number" value="0" />
						<button class="mui-btn mui-btn-numbox-plus" type="button">+</button>
					</div> -->
				</div>
				<div class="clum autoHeight">
					<span class="name">故障描述</span>
					<div class="clear"></div>
					<textarea class="textarea" id="Remarks" placeholder="请输入故障描述"></textarea>
				</div>
				<div class="clum">
					<span class="name">用途<em class="t_red">*</em></span>
					<input type="hidden" id="Use_type" required="required" placeholder="请选择备件用途"/>
					<span class="txt">
						<button style="color: #a4a4a4;" id='userTypeSel' class="mui-btn-select" type='button'>请选择备件用途</button>
					</span>
					<!-- <input type="text" id="Use_type" class="d_ipt" required="required" placeholder="请输入用途" /> -->
				</div>
				<div class="clum">
					<span class="name">客户名</span>
					<input type="text" id="CustName" class="d_ipt" placeholder="请输入客户名" />
				</div>
				<div class="clum" style="height: auto; padding: 13px 0">
					<span class="name" style="line-height: initial;">所在地区<em class="t_red">*</em></span>
					<input type="hidden" id="Province" required="required" placeholder="请选择有效所在地区" />
					<input type="hidden" id="City" placeholder="请选择有效所在地区" />
					<input type="hidden" id="AreaNo" placeholder="请选择有效所在地区" />
					<span class="txt" style="height: auto; line-height: initial;">
						<button style="color: #a4a4a4; line-height: initial; white-space: normal;text-align: right;" id='showCityPicker3' class="mui-btn-select" type='button'>请选择所在地区</button>
					</span>
				</div>
				<div class="clum">
					<span class="name">详细地址</span>
					<input type="text" id="shipaddress" class="d_ipt" placeholder="请输入详细地址" />
				</div>
				<div class="clum">
					<span class="name">邮编</span>
					<input type="text" id="postcode" class="d_ipt" placeholder="请输入邮编" />
				</div>
				<div class="clum">
					<span class="name">联系人</span>
					<input type="text" id="contactor" class="d_ipt" placeholder="请输入联系人" />
				</div>
				<div class="clum">
					<span class="name">电话</span>
					<input type="text" id="telephone" class="d_ipt" placeholder="请输入电话" />
				</div>
				<div class="clum">
					<span class="name">手机<em class="t_red">*</em></span>
					<input type="text" id="Mobile" class="d_ipt" required="required" placeholder="请输入手机" />
				</div>
				<div class="btn_box">
					<div class="btn_box">
						<span class="fl" id="cancelBtn">取消</span>
						<span class="fr red" id="submitBtn">保存</span>
					</div>
				</div>
				<div style="height: 50px;"></div>
			</div>
		</div>

		<!-- js代码 start -->

		<!-- 基础js start -->
		<script src="../../../js/mui.min.js"></script>
		<script src="../../../js/utils/h.js"></script>
		<script src="../../../js/constant.js"></script>
		<script src="../../../js/common.js"></script>
		<script src="../../../js/utils/base64.js"></script>
		<script src="../../../js/utils/md5.js"></script>
		<script src="../../../js/utils/sortMap.js"></script>
		<script src="../../../js/utils/http.js"></script>
		<script src="../../../js/app.js"></script>
		<!-- 基础js end -->
		<script src="../../../js/utils/rolldate.min.js"></script>
		<script src="../../../js/mui.picker.js"></script>
		<script src="../../../js/mui.poppicker.js"></script>
		<script src="../../../js/city.data-3.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../../js/fse/service/report/fieldservice/spareparts/spareparts.js"></script>
		<script type="text/javascript" charset="utf-8">
			var applyPartsKey = "FSE_ITEM_KEY_APPLY_PARTS";
			var addressData;
			var Area;
			//用途数据
			var data1=[{"text":"保外借用","value":"1"},{"text":"保内借用","value":"2"},{"text":"服务合同","value":"4"}];
			var data2=[{"text":"个人维修工具","value":"6"},{"text":"备件库存","value":"7"},{"text":"耗材销售","value":"8"}];
			var data3=[{"text":"返库维修","value":"5"}];
			
			(function($, doc) {
				$.init();

				var _getParam = function(obj, param) {
					return obj[param] || '';
				};

				$.plusReady(function() {
					// 上页参数
					var self = plus.webview.currentWebview();
					var wo = self.opener();
					var Type = self.Type;
					var khInfo = self.khInfo;
					var emergencyValue = khInfo.emergencyValue;//非工作时间紧急发货
					if (khInfo.CustName) {
						//客户名
						if (khInfo.CustName) {
							h("#CustName").val(khInfo.CustName);
						}
						//邮编
						if (khInfo.postcode) {
							h("#postcode").val(khInfo.postcode);
						}
						//联系人
						if (khInfo.contactor) {
							h("#contactor").val(khInfo.contactor);
						}
						//电话
						if (khInfo.telephone) {
							h("#telephone").val(khInfo.telephone);
						}
						//手机
						if (khInfo.Mobile) {
							h("#Mobile").val(khInfo.Mobile);
						}
						if (khInfo.Province) {
							h("#Province").val(khInfo.Province);
						}
						if (khInfo.City) {
							h("#City").val(khInfo.City);
						}
						if (khInfo.shipaddress) {
							h("#shipaddress").val(khInfo.shipaddress);
						}
						if (khInfo.City != "" && khInfo.Province != "") {
							h("#showCityPicker3").html(`${khInfo.Province} ${khInfo.City}`);
							// h("#showCityPicker3").html(khInfo.Province + " " + khInfo.City);
						}
						if (emptyStr(khInfo.postCode) != "") {
							h("#postcode").val(khInfo.postCode);
						}
						
					}
					
					// 获取cc省市区数据（f**k!!又不用cc了）
					// getAddressData();
					//用途下拉内容判断依据
					var Type = "";
					//获取上一页参数
					var self = plus.webview.currentWebview();
					var partNo = self.partNo; //备件号
					var itemIndex = self.itemIndex; //中修改备件所在缓存数组中的下标
					//获取缓存备件
					var items = h.getItem(applyPartsKey);
					var itemsArr = new Array();
					if (items) {
						itemsArr = JSON.parse(items);
					}
					
					
					
					//回显参数
					h("#Part_no").val(partNo);
					h("#itemIndex").val(itemIndex);
					//回显详情
					if (itemsArr.length > 0) {
						var info = itemsArr[itemIndex];
						//用途下拉内容判断依据
						// console.log(JSON.stringify(info))
						Type = khInfo.Type;
						// 回显到表单
						h("#Part_qty").val(emptyStr(info.Part_qty));
						h("#Part_name").val(emptyStr(info.Part_name));
						h("#Use_type").val(emptyStr(info.Use_type))
						h("#CustName").val(emptyStr(info.CustName));
						h("#shipaddress").val(emptyStr(info.shipaddress));
						h("#postcode").val(emptyStr(info.postcode));
						h("#contactor").val(emptyStr(info.contactor));
						h("#telephone").val(emptyStr(info.telephone));
						h("#Mobile").val(emptyStr(info.Mobile));
						h("#showCityPicker3").html(emptyStr(info.showCityPicker3));
						h("#AreaNo").val(emptyStr(info.AreaNo));
						Area = info.Area
						// 用途回传
						console.log(JSON.stringify(info))
						if (!info.Use_type){
							// var selectJson = data1.find(function(e){
							// 	return e.text.indexOf(khInfo.isWarranty) !== -1
							// })
							// h('#userTypeSel').html(selectJson.text)
							// h('#Use_type').val(selectJson.value)
						}else{
							var data;
							if (info.Type==1) {
								data = data1
							} else if (info.Type==2) {
								data = data2
							} else {
								data = data3
							}
							var selectJson = data.find(function(e){
								return e.value == info.Use_type
							})
							
							h('#userTypeSel').html(selectJson.text)
							h('#Use_type').val(selectJson.value)
						}
					}
					// 备件用途下拉
					h('#userTypeSel').tap(function() {
						document.activeElement.blur();
						app.hideSoftKeybord();
						var userPicker = new $.PopPicker();
						if (emergencyValue == 1) {
							var data4=[{"text":"紧急借货","value":"9"}];
							userPicker.setData(data4);
						} else {
							if (Type==1) {
								userPicker.setData(data1);
							} else if (Type==2) {
								userPicker.setData(data2);
							} else {
								userPicker.setData(data3);
							}
						}
						userPicker.show(function(items) {
							h('#userTypeSel').html(items[0].text)
							h('#Use_type').val(items[0].value)
							userPicker.dispose()
						});
					});
					// 收货地址二级联动
					h('#showCityPicker3').tap(function() {
						var cityPicker3 = new $.PopPicker({
							// layer: 3
							layer: 3 // 设为2级联动
						});
						// cityPicker3.setData(addressData);
						cityPicker3.setData(cityData3);
						cityPicker3.show(function(items) {
							h("#showCityPicker3").html( _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text'));
							h("#Province").val(_getParam(items[0], 'value'));
							h("#City").val(_getParam(items[1], 'value'));
							// h("#shipaddress").val(_getParam(items[2], 'text'));
							// 接口缺少字段 区  暂时用这个代替验证
							h("#AreaNo").val(_getParam(items[2], 'value'));
							Area = _getParam(items[2], 'value')
						});
					});
					//取消
					h("#cancelBtn").tap(function() {
						self.close();
					})
					//保存
					h('#submitBtn').tap(function() {
						var formObj = {}; //POST参数对象
						var subFlag = true;
						mui("#form_info input, #form_info textarea").each(function(index, item) {
							var value = item.value;
							var name = item.getAttribute("id");
							var required = item.getAttribute("required");
							if (required && !value) {
								subFlag = false;
								app.toast(item.getAttribute("placeholder"));
								return false;
							}
							formObj[name] = value; //参数赋值
						});
						
						if (subFlag) {
							// 合法省市区地址校验
							var dizhi = h("#showCityPicker3").html();
							var ProvinceVal = h('#Province').val();
							var area = h("#AreaNo").val();
							
							// 接口缺少字段 区  暂时用这个代替验证
							if(!area){
								app.toast("请选择有效所在地区");
								return false; 
							}
							// if (dizhi.indexOf("省") < 0 && dizhi.indexOf("市") < 0 && dizhi.indexOf("区") < 0 && dizhi.indexOf("县") < 0) {
							// 	app.toast("请选择有效所在地区");
							// 	return false; 
							// }
							// shipaddress
							formObj["shipaddress"] = h("#shipaddress").val();
							// 修改页回显用
							formObj["Type"] = Type; //修改页也需要根据type显示用途
							formObj["userTypeSel"] = h("#userTypeSel").html(); //选中用途名
							formObj["showCityPicker3"] = h("#showCityPicker3").html(); //选中地址名
							// formObj["Area"] = h("#Area").val(); //选中地址名
							var telephone = h("#telephone").val();
							if(telephone && !/^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/.test(telephone)){ 
								app.toast("电话号码有误");  
								return false; 
							}
							var Mobile = h("#Mobile").val();
							if(!(/^[1][3,4,5,7,8][0-9]{9}$/.test(Mobile))){ 
								app.toast("手机号码有误");  
								return false; 
							}
							// 存入缓存
							console.log(JSON.stringify(formObj))
							if (itemsArr.length > 0) {
								itemsArr[itemIndex] = formObj;
								h.setItem(applyPartsKey, JSON.stringify(itemsArr));
								app.toast("保存成功");
								wo.evalJS('showItems()')
								self.close();
							} else {
								app.toast("保存失败");
							}
						}
					});
					
					// 查详情
					// h('#selectDetailsBtn').tap(function() {
					// 	var enginnerId = app.getUser().enginnerId;
					// 	var Part_no = h("#Part_no").val();
						
					// 	app.showWaiting('加载中...')
						
					// 	cleanInput();
						
					// 	ajaxPost(BASE_URL + "cc/spareParts/queryPartByFseIdPost", {
					// 		"CustName": "",
					// 		"Status": "",
					// 		"Part_no": Part_no,
					// 		"enginner_id": enginnerId
					// 	}, function(data) {
					// 		app.closeWaiting()
					// 		if (data.code === 200) {
					// 			var str = '';
					// 			var dataObj = JSON.parse(data.data);
					// 			var list = dataObj.record;
					// 			if (list.length > 0) {
					// 				var info = list[0];
					// 				console.log(JSON.stringify(info))
					// 				// //备件号
					// 				// h("#Part_no").val(info.Part_no);
					// 				//备件名称
					// 				h("#Part_name").val(info.Part_name);
					// 				//用途
					// 				h("#Use_type").val(info.Use_type);
					// 				//客户名
					// 				h("#CustName").val(info.CustName);
					// 				//所在地区
					// 				h("#shipaddress").val(partStatusName(info.shipaddress));
					// 				//邮编
					// 				h("#postcode").val(info.postcode);
					// 				//联系人
					// 				h("#contactor").val(info.contactor);
					// 				//电话
					// 				h("#telephone").val(info.telephone);
					// 				//手机
					// 				h("#Mobile").val(info.Mobile);
					// 			} else {
					// 				app.toast("查询无结果")
					// 			}
					// 		} else {
					// 			app.toast(data.msg)
					// 		}
					// 	}, function(errorMsg) {
					// 		app.closeWaiting()
					// 		app.toast(errorMsg)
					// 	});
					// });
				});
			}(mui, document));
			
			function getAddressData() {
				ajaxGet(BASE_URL + "cc/spareParts/ccAddressData",{
					"enginner_id": ""
				}, function(data) {
					// console.log(JSON.stringify(data));
					if (data.code === 200) {
						addressData = data.data;
					} else {
						// app.toast(data.msg)
					}
				}, function(errorMsg) {
					// app.toast(errorMsg)
				});
			}
			
			// 清空输入框
			function cleanInput() {
				//备件名称
				h("#Part_name").val("");
				//用途
				h("#Use_type").val("");
				//客户名
				h("#CustName").val("");
				//所在地区
				h("#shipaddress").val("");
				//邮编
				h("#postcode").val("");
				//联系人
				h("#contactor").val("");
				//电话
				h("#telephone").val("");
				//手机
				h("#Mobile").val("");
			}
		</script>
		<!-- js代码 end -->
	</body>
</html>
